/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mattespill.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import mattespill.*;

/**
 *
 * @author Erik
 */
public class GUIHighscore extends JPanel {

    private Oversikt oversikt;
    private JTable tabell;
    private JButton tilbake = new JButton("Tilbake");
    private final String [] kolonneNavn = {"Nr","Elevnavn","Poeng"};

    public GUIHighscore(Oversikt o) {
        this.oversikt = o;

        DefaultTableModel model = new DefaultTableModel();
        tabell = new JTable(null,kolonneNavn);
        JScrollPane scrollPane = new JScrollPane(tabell);

        scrollPane.setBackground(new Color(255,255,255,150));
        this.setBackground(new Color(255,255,255,150));
        scrollPane.getViewport().setOpaque(false);
        tabell.setModel(model);
        tabell.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
       

        model.setColumnIdentifiers(new String [] {"Nr","Elevnavn","Poeng"});
        
        for(int i=0;i<oversikt.getHighscore().size();i++){
            model.addRow(new String[] {""+(i+1),""+oversikt.getHighscore().get(i).getBruker().getNavn(),""+oversikt.getHighscore().get(i).getStatus().getPoeng()});
            if(i == 9){
                break; // KUN TOPP-10
            }
        }
        TableColumn col = tabell.getColumnModel().getColumn(0);
        col.setPreferredWidth(30);
        TableColumn col2 = tabell.getColumnModel().getColumn(1);
        col2.setPreferredWidth(319);
        TableColumn col3 = tabell.getColumnModel().getColumn(2);
        col3.setPreferredWidth(100);
        setLayout(new BorderLayout());
        add(scrollPane,BorderLayout.CENTER);

        tilbake.addActionListener(new Knapplytter());
        add(new Panelet(),BorderLayout.SOUTH);
    }
    private class Panelet extends JPanel {
        public Panelet() {
            setBackground(new Color(255,255,255,150));
            add(tilbake);
        }
    }
    private class Knapplytter implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            JButton hendelse = (JButton)e.getSource();
            if(hendelse.equals(tilbake)){
                GUIFortsett g = new GUIFortsett(oversikt);
                JPanel mainPanel = (JPanel)getParent();
                mainPanel.removeAll();// dette is the sub-panel
                mainPanel.repaint();
                mainPanel.add(g);
                mainPanel.revalidate();
                g.repaint();
            }
        }

    }
}



